﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using XZL.GM.Bll;
using XZL.GM.Dal;

public partial class Admin_Course_CourseEdit : AdminBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            BindDDL();
            BindCourseList();
        }
    }

    /// <summary>
    /// 绑定下拉列表
    /// </summary>
    private void BindDDL()
    { 
       ddlCourseType.DataSource=CourseInfoBiz.GetCourseTypeList();
       ddlCourseType.DataTextField = "CourseTypeName";
       ddlCourseType.DataValueField = "CourseTypeID";
       ddlCourseType.DataBind();

       ddlTeacherID.DataSource = CourseInfoBiz.GetCourseTeacherList();
       ddlTeacherID.DataTextField = "TeacherName";
       ddlTeacherID.DataValueField = "TeacherID";
       ddlTeacherID.DataBind();
    }


    private void BindTeamList()
    {
        int? count = 0;
        int i = Convert.ToInt32(ddlCourseType.SelectedValue);
        ddlTeam.DataSource = CourseInfoBiz.GetCourseTeamList(i, 1, 1000, ref count);
        ddlTeam.DataTextField = "TeamName";
        ddlTeam.DataValueField = "TeamID";
        ddlTeam.DataBind();
    }


    /// <summary>
    /// 保存数据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>

    protected void btnOK_Click(object sender,EventArgs e)
    {

        string price = txtPrice.Text.Trim();

        if (txtCourseName.Text.Trim() == "")
        {
            lblWarn.Text = "请输入课程名称！";
            txtCourseName.Focus();
            return;
        }

        //判断是否为时间格式
        string sTime = txtStartTime.Text.Trim();
        DateTime dtDate;
        if (DateTime.TryParse(sTime, out dtDate))
        {
            txtStartTime.Focus();
            
        }
        else
        {
            lblWarn.Text = "开始时间(格式:2018-01-01 20:30:00)！";
            return;
        }


        double result;
        bool b = double.TryParse(price, out result);
        if (b)
        {
            lblWarn.Text = "";
        }
        else
        {
            lblWarn.Text = "请输入正确的课程价格！";
            txtPrice.Focus();
            return;
        }

        string sSort = txtSort.Text.Trim();
        int resulter;
        bool f = Int32.TryParse(sSort, out resulter);
        if (f)
        {
            lblWarn.Text = "";
        }
        else
        {
            lblWarn.Text = "请输入正确排序数字！";
            txtSort.Text = "0";
            txtSort.Focus();
            return;
        }


        int status = 1;
        if (rb2.Checked == true)
        {
            status = -1;
        }

        int isInviteHelp = 1;
        if (is2.Checked == true)
        {
            isInviteHelp = 0;
        }


        string id = Request.QueryString["CourseID"];
        int courseID = Convert.ToInt32(id);
        string courseName = txtCourseName.Text.Trim();

        DateTime startTime = Convert.ToDateTime(txtStartTime.Text.Trim());

        int courseType = Convert.ToInt32(ddlCourseType.Text.Trim());
        int courseTeamID = Convert.ToInt32(ddlTeam.Text.Trim());
        int teacherID = Convert.ToInt32(ddlTeacherID.Text.Trim());
        int ischarge = Convert.ToInt32(ddlIsCharge.Text.Trim());
        int islive = Convert.ToInt32(ddlIsLive.Text.Trim());
        string videoLink = txtVideoLink.Text.Trim();
        int sort = Convert.ToInt32(txtSort.Text.Trim());

        string courseImg = lblFileNameImg.Text.Trim();
        string courseInfoImg = lblFileNameInfoImg.Text.Trim();
        string courseExplain = txtCourseExplain.Text.Trim();
        string curseIntroduce = txtCourseIntroduce.Text.Trim();

        CourseInfo courseInfo = new CourseInfo()
        {
            CourseID= courseID, 
            CourseName = courseName,

            CourseType=courseType,
            CourseTeamID=courseTeamID,
            TeacherID=teacherID,
            IsCharge = ischarge,
            IsLive=islive,
            VideoLink =videoLink,

            Price = Convert.ToDouble(price),
            StartTime = startTime,
         
            Sort=sort,
            CourseImg = courseImg,
            CourseInfoImg=courseInfoImg,
            CourseExplain=courseExplain,
            CourseIntroduce = curseIntroduce,
            Status = status,
            IsInviteHelp = isInviteHelp
        };

        bool res = CourseInfoBiz.CourseUpdate(courseInfo);
        if (res == true)
        {
            lblWarn.Text = "课程修改成功！";
            lblWarn.ForeColor = System.Drawing.Color.Green;
            btnOK.Enabled = false;
        }
        else
        {
            lblWarn.Text = "课程修改失败！";
            lblWarn.ForeColor = System.Drawing.Color.Red;
        }


        Response.Redirect("CourseList.aspx");

    }

    /// <summary>
    /// 绑定课程信息
    /// </summary>
    private void BindCourseList()
    { 
    string  str=Request.QueryString["CourseID"];
        if(str!= null)
        {
            int courseID = Int32.Parse(str);
            Proc_CourseInfo_SelectInfoResult course = CourseInfoBiz.GetCourseInfo(courseID);
         
            if(course!=null)
            {

            txtCourseName.Text=course.CourseName;
            txtStartTime.Text = course.StartTime.ToString();          
            txtPrice.Text = course.Price.ToString();        
            ddlCourseType.SelectedValue = course.CourseType.ToString();
           
            BindTeamList();
            ddlTeam.SelectedValue = course.CourseTeamID.ToString();
            ddlTeacherID.SelectedValue = course.TeacherID.ToString();
            ddlIsCharge.SelectedValue = course.IsCharge.ToString();
            ddlIsLive.SelectedValue = course.IsLive.ToString();
            txtVideoLink.Text = course.VideoLink;  
            txtSort.Text = course.Sort.ToString();
            txtCourseExplain.Text = course.CourseExplain;
            txtCourseIntroduce.Text = course.CourseIntroduce;

            string imgPath = System.Configuration.ConfigurationManager.AppSettings["ImgPath"];
            if (course.CourseImg == "" || course.CourseImg == null)
           {
                
           }
           else
           {
               imgSignatureone.ImageUrl = imgPath + course.CourseImg;
               lblFileNameImg.Text = course.CourseImg;
           }

            if (course.CourseInfoImg == "" || course.CourseInfoImg == null)
            {

            }
            else
            {
                imgSignaturetwo.ImageUrl = imgPath + course.CourseInfoImg;
                lblFileNameInfoImg.Text = course.CourseInfoImg;
            }


            if (course.Status == 1)
            {
                rb1.Checked = true;
            }
            else
            {
                rb2.Checked = true;
            }


            if (course.IsInviteHelp == 1)
            {
                is1.Checked = true;
            }
            else  
            {
                is2.Checked = true;
            }

         }

        }
    
    }

    /// <summary>
    /// 上传课程列表图片
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>

    protected void btnUpLoad_Click(object sender, EventArgs e)
    {
        if (txtSelectFileOne.HasFile)
    {
        HttpPostedFile postedFile = txtSelectFileOne.PostedFile;
        string fileName = System.IO.Path.GetFileName(postedFile.FileName);
            if(fileName!="")
            {
                string fileExtension = System.IO.Path.GetExtension(fileName);
                string newName = Guid.NewGuid().ToString();
                string imgPath = System.Configuration.ConfigurationManager.AppSettings["ImgPath"];
                string path = System.Web.HttpContext.Current.Request.MapPath(imgPath) + newName + fileExtension;

                lblFileNameImg.Text = newName + fileExtension;         //列表图片          
                ///'可根据扩展名字的不同保存到不同的文件夹
                ///注意：可能要修改你的文件夹的匿名写入权限。
                postedFile.SaveAs(path);
                lblUploadInfoone.Text = "上传列表图片成功";           
                imgSignatureone.ImageUrl = imgPath + newName + fileExtension;    //列表图片            
            }
    }
    }


    /// <summary>
    /// 上传课程详情图片
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>

    protected void btnUpLoadone_Click(object sender, EventArgs e)
    {
        if (txtSelectFileTwo.HasFile)
        {
            HttpPostedFile postedFile = txtSelectFileTwo.PostedFile;
            string fileName = System.IO.Path.GetFileName(postedFile.FileName);
            if (fileName != "")
            {
                string fileExtension = System.IO.Path.GetExtension(fileName);
                string newName = Guid.NewGuid().ToString();
                string imgPath = System.Configuration.ConfigurationManager.AppSettings["ImgPath"];
                string path = System.Web.HttpContext.Current.Request.MapPath(imgPath) + newName + fileExtension;

                lblFileNameInfoImg.Text = newName + fileExtension;     //详情图片
                ///'可根据扩展名字的不同保存到不同的文件夹
                ///注意：可能要修改你的文件夹的匿名写入权限。
                postedFile.SaveAs(path);              
                lblUploadInfotwo.Text = "上传详情图片成功";            
                imgSignaturetwo.ImageUrl = imgPath + newName + fileExtension;    //详情图片
            }
        }
    }

    /// <summary>
    /// 返回
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("CourseList.aspx");
    }


    protected void ddlCourseType_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindTeamList();
    }
}